package vip.xiaonuo.biz.modular.Englishword;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import vip.xiaonuo.biz.core.config.QianWenConfig;
import vip.xiaonuo.biz.modular.Englishword.param.DayWordParam;
import vip.xiaonuo.biz.modular.Englishword.result.QianWenResult;
import vip.xiaonuo.biz.modular.Englishword.service.impl.BizCet43ServiceImpl;
import vip.xiaonuo.biz.modular.wordarticle.entity.BizWordArticle;
import vip.xiaonuo.biz.modular.wordarticle.service.BizWordArticleService;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

@Component
@Slf4j
public class AutoStart implements CommandLineRunner {
    @Autowired
    QianWenConfig qianWenConfig;
    @Autowired
    BizCet43ServiceImpl bizCet43Service;
    @Autowired
    BizWordArticleService wordArticleService;

    @Override
    public void run(String... args) throws Exception {
        // 计时
        System.out.println("开始自动生成文章");
//        for (int i = 0; i < 10000; i++) {
//            try{
//                Map<Integer, String> simpleMap = bizCet43Service.dayWordSimpleMap(new DayWordParam(40));
//                Set<Integer> idSets = simpleMap.keySet();
//                String idStr = JSON.toJSONString(idSets);
//                Collection<String> wordSet = simpleMap.values();
//                String valueStr = JSON.toJSONString(wordSet);
//
//                String wordStr = StrUtil.join(",", wordSet);
//
//                QianWenResult qianWenResult = qianWenConfig.sendWordTxt(wordStr, 3);
//                BizWordArticle bizWordArticle = new BizWordArticle();
//                bizWordArticle.setArticle(qianWenResult.getChoices().get(0).getMessage().getContent())
//                        .setWordId(idStr)
//                        .setHeadword(valueStr);
//                wordArticleService.save(bizWordArticle);
//                log.info("第{}次生成文章", i);
//                Thread.sleep(1000);
//                log.info("第{}次休息结束", i);
//            }
//            catch (Exception e){
//                log.error("自动生成文章出错", e);
//                Thread.sleep(5000);
//                log.error("休息5s结束");
//            }
//        }
//        System.out.println("自动生成文章结束");
    }
}
